/**
 * 获取host域名
 * @returns
 */
function hostUrl() {
    return location.protocol+'//'+location.host;
}

/**
 * 上传图片
 * @param file 需要上传的文件
 * @param callback 回调函数 接受参数url
 */
function upLoadImg(file, callback)
{
    var file = file;
    var form = new FormData();
    form.append('files', file);
    $.ajax({
        type: 'post',
        dataType: 'json',
        url: hostUrl() + '/files/upload_image',
        data: form,
        cache: false,
        contentType: false,
        processData: false,
        success: function(res) {
            console.log(res);
            if (res.status) {
                if (typeof callback === 'function') {
                    callback(res.data);
                }
            } else {
                layer.msg(res.msg);
            }
        },
        error: function() {
            layer.msg('网络错误');
        }
    });
}

/**
 * 发送短信验证码倒计时
 * @param obj
 * @param count
 */
function jump(obj, count) {
    obj.attr('disabled', 'disabled').attr('issend', 0).text(count+'秒');
    window.setTimeout(function(){
        count--;
        if(count > 0) {
            obj.text(count+'秒');
            jump(obj, count);
        } else {
            obj.text('重新发送').removeAttr('disabled').attr('issend', 1);
        }
    }, 1000);
}

/**
 * app下载地址链接，直接跳转
 * @param android
 * @param ios
 */
function linkDownLoad(android, ios)
{
    if (!arguments[0]) android = 'http://a.app.qq.com/o/simple.jsp?pkgname=com.saker.app.huhu';
    if (!arguments[1]) ios     = 'itms-apps://itunes.apple.com/cn/app/id573784372?mt=8';

    var u = navigator.userAgent;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    if (isAndroid) {
        window.location.href = android;
    } else if (isIOS) {
        window.location.href = ios;
    } else {
        window.location.href = android;
    }

    return;
}

jQuery(document).ready(function($) {
    /* 重置搜索条件 */
    $('.reset_button_search').click(function(){
        var currentUrl = window.location.href;
        if (currentUrl.indexOf('?') != '-1') {
            var currentUrlPre = currentUrl.split('?')[0];
            window.location.href = currentUrlPre;
        } else {
            $('.form-search')[0].reset();
        }
    });
    
    $('.line-form').validate();

    //CKedit 编辑器 js
    if ($('textarea.textarea-multipart-edit').size() > 0) {
        KindEditor.create('textarea.textarea-multipart-edit', {
            width : '73%',
            height : '250px',
            filterMode: false
        });
    }
    
    //wysiwyg 编辑器 js
    if ($('textarea.textarea-wysiwyg-edit').size() > 0) {
        $('.textarea-wysiwyg-edit').wysiwyg();
    }
    
    //table list 全选或全不选操作
    jQuery('#sample_1 .group-checkable, #sample_1 .group-checkable2').change(function () {
        var set = jQuery(this).attr('data-set');
        var checked = jQuery(this).is(':checked');
        jQuery(set).each(function () {
            if (checked) {
                $(this).prop('checked', true);
            } else {
                $(this).attr('checked', false);
            }
        });
        jQuery.uniform.update(set);
    });
});


